//
//  CRDialogueCell.m
//  CRChatRoom
//
//  Created by lanou3g on 15/11/19.
//  Copyright © 2015年 SecondGroup. All rights reserved.
//

#import "CRDialogueCell.h"
#import "CRDialogue.h"
#import "UIImageView+WebCache.h"
@interface CRDialogueCell ()
{
    UIButton *_timeBt;
    UIImageView *_iconImageV;
    UIButton *_contentBt;
    UIImageView *_pictureView;
    
}
@property (nonatomic, strong) NSString *contenStr;
@end


@implementation CRDialogueCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self setSubView];
    }
    return  self;
}
- (void)setSubView
{
    //创建时间Button
    _timeBt = [[UIButton alloc] init];
    [_timeBt setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _timeBt.titleLabel.font = DTimeFont;
    _timeBt.enabled = NO;
    [self.contentView addSubview:_timeBt];
    
    //创建头像
    _iconImageV = [[UIImageView alloc] init];
    [self.contentView addSubview:_iconImageV];
    
    //图片
    _pictureView = [[UIImageView alloc] init];

    
    //创建内容按钮
    _contentBt = [[UIButton alloc] init];
    [_contentBt setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _contentBt.titleLabel.font = [UIFont systemFontOfSize:14];
    _contentBt.titleLabel.numberOfLines = 0;
       [self.contentView addSubview:_contentBt];
    
    [_contentBt addSubview:_pictureView];
}

//重写Frame的set方法
- (void)setCellFrame:(CRDialogueFrame *)cellFrame
{
    _cellFrame = cellFrame;
    CRDialogue *dialogue = cellFrame.dialogue;
    self.contenStr = dialogue.content;
    //1.设置时间
    [_timeBt setTitle:dialogue.currentTime forState:UIControlStateNormal];
    _timeBt.frame = cellFrame.currentTimeRect;
    
    //2.设置头像
    [_iconImageV sd_setImageWithURL:[NSURL URLWithString:dialogue.icon] placeholderImage:[UIImage imageNamed:@"r"]];
    _iconImageV.frame = cellFrame.iconRect;
    _iconImageV.layer.cornerRadius = _iconImageV.width * 0.5;
    _iconImageV.clipsToBounds = YES;
    
    //3.设置内容
    [_contentBt setTitle:dialogue.content forState:UIControlStateNormal];
    _contentBt.contentEdgeInsets = UIEdgeInsetsMake(DContBottom, DContLeft, DContTop, DContright);
    _contentBt.frame = cellFrame.contentRect;
    if (dialogue.dialogueType == sendDType) {
        _contentBt.contentEdgeInsets = UIEdgeInsetsMake(DContTop, DContright, DContBottom, DContLeft);
    }
    
    //图片
    _pictureView.image = [UIImage imageNamed:dialogue.picture];
    _pictureView.frame = cellFrame.pictureRect;

    //常态 nomal，选中状态 focused
    UIImage *normal, *focused;
    if (dialogue.dialogueType == sendDType) {
        normal = [UIImage imageNamed:@"chat_send_normal"];
        //使用可调整大小的图像与
        normal = [normal stretchableImageWithLeftCapWidth:normal.size.width * 0.5
                                             topCapHeight:normal.size.height * 0.7];
        focused = [UIImage imageNamed:@"chat_send_focused"];
        focused = [focused stretchableImageWithLeftCapWidth:focused.size.width * 0.5
                                               topCapHeight:focused.size.height *0.7];
    }else{
        normal = [UIImage imageNamed:@"chat_receive_normal"];
        normal = [normal stretchableImageWithLeftCapWidth:normal.size.width * 0.5
                                             topCapHeight:normal.size.height * 0.7];
        focused = [UIImage imageNamed:@"chat_receive_focused"];
        focused = [focused stretchableImageWithLeftCapWidth:focused.size.width * 0.5
                                               topCapHeight:focused.size.height *0.7];
    }
    [_contentBt setBackgroundImage:normal forState:UIControlStateNormal];
    [_contentBt setBackgroundImage:focused forState:UIControlStateHighlighted];
    [_contentBt addTarget:self action:@selector(longPress:) forControlEvents:UIControlEventTouchDown];
}
//长按调出菜单，并成为第一响应者，设置菜单
- (void)longPress:(UILongPressGestureRecognizer *)longPress
{
    [self becomeFirstResponder];
    UIMenuController *menu = [UIMenuController sharedMenuController];
    [menu setTargetRect:self.bounds inView:self];
    [menu setMenuVisible:YES animated:YES];
}
//响应平台方法
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    //响应方法
    if (action == @selector(copy:)) {
        return YES;
    }
    return [super canPerformAction:action withSender:sender];
}
//响应方法
- (void)copy:(id)sender
{
    [[UIPasteboard generalPasteboard] setString:self.contenStr];
}

//能成为第一响应者
-(BOOL)canBecomeFirstResponder
{
    return YES;
}
- (void)awakeFromNib{
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

}

@end
